<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="left">
        <img src="@/assets/imgs/shop.png" />
        <span>电商后台管理系统</span>
      </div>
      <div class="right">
        <span>欢迎您：{{ userInfo.username }}</span>
        <el-button size="mini" type="danger" @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏菜单 -->
        <div class="toggle-button" @click="isCollapse=!isCollapse">|||</div>
        <el-menu :default-active="$route.path" unique-opened router :collapse="isCollapse" :collapse-transition="false">
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
                <i class="el-icon-menu"></i>
                {{ subItem.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      userInfo: null,
      menuList: [],
      iconObj: {
        '201':'el-icon-s-home',
        '125':'el-icon-user-solid',
        '103':'el-icon-s-promotion',
        '101':'el-icon-s-goods',
        '102':'el-icon-s-order'
      },
      isCollapse:false
    };
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.getMenuList()
  },
  methods: {
    logout() {
      this.$confirm("确定要退出登录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sessionStorage.clear();
          this.$router.push("/login");
        })
        .catch(() => {
          //   this.$message({
          //     type: "info",
          //     message: "取消退出",
          //   });
        });
    },
    async getMenuList(){
        const {data:res} = await this.$http.get('menus')
        if(res.meta.status !== 200){
            return this.$message.error(res.meta.msg);
        }
        this.menuList = res.data
        // console.log(this.menuList)
    }
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    display: flex;
    background: pink;
    height: 50px !important;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      align-items: center;
      img {
        width: 35px;
        margin-right: 20px;
      }
      sapn {
        font-size: 20px;
      }
    }
    .right {
      span {
        margin-right: 10px;
      }
    }
  }
  .el-aside{
    .el-menu {
        border-right: 0;
    }
    .toggle-button {
        background: #FFB6C1;
        font-size: 10px;
        line-height: 24px;
        letter-spacing: 0.3rem;
        text-align: center;
        cursor: pointer;
    }
  }
  .el-main{
    background: #eaedf1;
  }
}
</style>
